RAIS  3.2
C:/Projekte/RAIS/Specific Masks/Import.aspx.cs
Go to the documentation of this file.
00001 
00028 using System;
00029 using System.Data;
00030 using System.Configuration;
00031 using System.Collections;
00032 using System.Web;
00033 using System.Web.Security;
00034 using System.Web.UI;
00035 using System.Web.UI.WebControls;
00036 using System.Web.UI.WebControls.WebParts;
00037 using System.Web.UI.HtmlControls;
00038 
00039 using RAIS.UI.Common;
00040 using RAIS.Common.UserManagement;
00041 using IcSRS_Import;
00042 using System.Collections.Generic;
00043 using System.Drawing;
00044 using System.Data.SqlClient;
00045 //using RAIS.ApplicationLogicLayer;
00046 
00047 namespace RAIS.UI.SpecificMasks
00048 {
00052     public partial class Import : System.Web.UI.Page
00053     {
00054         private IcSRS_Import.IcSRS_Import import;
00055 
00056                 #region Properties
00057 
00061                 protected UserData CurrentUser
00062                 {
00063                         get { return (UserData)this.Session[UI.Common.Constants.CURRENT_USER_TAG]; }
00064                 }
00065 
00066                 #endregion
00067                 #region Event handlers
00068 
00069         protected void Page_Load(object sender, EventArgs e)
00070         {
00071                         this.Response.Cache.SetNoStore();
00072                         if (this.CurrentUser == null)
00073                                 UI.Common.AccessManagement.SignOut(this);
00074 
00075             if (!this.IsPostBack)
00076             {
00077                 this.LBLusernameNoTranslation.Text = this.CurrentUser.Name;
00078                 UI.Common.PageManagement.InitializePage(this, this.TreeView1);
00079                 import = new IcSRS_Import.IcSRS_Import();
00080 
00081                 // Create selected item counter which is used for the import-button text and the LBLsearch label
00082                 Session["SelectedItemCount"] = 0;
00083 
00084                 /* Store the Search Result as a DataTable in a session attribute.
00085                  * This is mandatory for the PageIndexChanging Event!
00086                  */
00087                 Session["SearchResult"] = import.getSearchResult(Request["search"], Request["type"].ToString()
00088                                         , this.Session[RAIS.UI.Common.Constants.IcSRS_LOGIN_TAG].ToString()
00089                                         , this.Session[RAIS.UI.Common.Constants.IcSRS_PASSWORD_TAG].ToString());
00090 
00091                 // Set GridView datasource
00092                 GVsearchResult.DataSource = (DataTable)Session["SearchResult"];
00093                 GVsearchResult.DataBind();
00094 
00095                 if (GVsearchResult.Rows.Count > 0) 
00096                     LBLsearch.Text = "Search result for: \"" + Request["search"] + "\" in " + Request["type"] + "s";
00097                 else // There are no search results
00098                 {
00099                     LBLsearch.Text = "Sorry there were no search results for \"" + Request["search"] + "\" in " + Request["type"] + "s";
00100                     BTNimport1.Visible = false;
00101                     BTNback.Visible = true;
00102                 }
00103             }
00104             this.LBLinput.ForeColor = RAIS.UI.Common.Constants.COLOR_OF_MENU_BAR_TEXT_HIGHLIGHT;
00105 //                      this.LBTNmessagebox.Visible = !(this.CurrentUser.FunctionalRole.Type == FunctionRole.FunctionRoleType.Guest);
00106             PageManagement.InitHelpMainMenu(LBTNdefault);
00107                 }
00108 
00109         protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
00110         {
00111             UI.Common.UserManagement.ProcessTreeViewOnRedirect(this.TreeView1);
00112         }
00113 
00114         protected void LBTNlogout_Click(object sender, EventArgs e)
00115         {
00116             UI.Common.AccessManagement.SignOut(this);
00117         }
00118 
00119         protected void BTNimportItems_Click(object sender, EventArgs e)
00120         {
00121             // import selected items
00122             switch (Request["Type"])
00123             {
00124                 case "Associated Equipment":
00125                     this.importSelectedDevices();
00126                     break;
00127                 case "Source":
00128                     this.importSelectedSources();
00129                     break;
00130                 case "Manufacturer":
00131                     this.importSelectedManufacturers();
00132                     break;
00133             }
00134         }
00135 
00136         protected void CBXitemChecked_CheckedChanged(object sender, EventArgs e)
00137         {
00138             GridViewRow currentRow = (GridViewRow)((Control)sender).Parent.Parent;
00139             if (((CheckBox)sender).Checked)
00140             {
00141                 BTNimport1.Enabled = true;
00142 
00143                 // Increase selectet item counter 
00144                 Session["SelectedItemCount"] = Convert.ToInt32(Session["SelectedItemCount"]) + 1;
00145 
00146                 // Sets the "Selected" column in the DATATABLE! to boolean TRUE
00147                 ((DataTable)Session["SearchResult"]).Rows[currentRow.RowIndex + (GVsearchResult.PageSize * GVsearchResult.PageIndex)][0] = true;
00148 
00149                 // Sets its backcolor to yellow, meaning that this item is selected
00150                 currentRow.BackColor = Color.FromArgb(255, 255, 180);
00151             }
00152             else
00153             {
00154                 // Decrease selected item counter
00155                 Session["SelectedItemCount"] = Convert.ToInt32(Session["SelectedItemCount"]) - 1;
00156 
00157                 // Checks if item counter equals zero. If so, disable import button
00158                 if (Convert.ToInt32(Session["SelectedItemCount"]) == 0)
00159                     BTNimport1.Enabled = false;
00160 
00161                 // Set row unchecked
00162                 ((DataTable)Session["SearchResult"]).Rows[currentRow.RowIndex + (GVsearchResult.PageSize * GVsearchResult.PageIndex)][0] = false;
00163 
00164                 // Set row color
00165                 if (currentRow.RowIndex % 2 == 0)
00166                     currentRow.BackColor = Color.FromArgb(239, 243, 251);
00167                 else
00168                     currentRow.BackColor = Color.White;
00169             }
00170 
00171             BTNimport1.Text = "Import selected items (" + Session["SelectedItemCount"] + ")";
00172         }
00173 
00174         protected void GVsearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
00175         {
00176             // Reset datasource
00177             GVsearchResult.DataSource = (DataTable)Session["SearchResult"];
00178 
00179             // Set new page index
00180             GVsearchResult.PageIndex = e.NewPageIndex;
00181 
00182             // Bind data to the GridView
00183             GVsearchResult.DataBind();
00184 
00185             // Set color of rows (yellow meaning checked)
00186             foreach (GridViewRow row in GVsearchResult.Rows)
00187             {
00188                 if (((CheckBox)row.Cells[0].FindControl("CBXitemChecked")).Checked)
00189                 {
00190                     row.BackColor = Color.FromArgb(255, 255, 180);
00191                 }
00192                 else if (row.RowIndex % 2 == 0)
00193                     row.BackColor = Color.FromArgb(239, 243, 251);
00194                 else
00195                     row.BackColor = Color.White;
00196             }
00197         }
00198 
00199         protected void GVsearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
00200         {
00201             /* Hides the "isSelected" data column of the "SearchResult"-DataTable which contains 
00202              * a boolean specifying whether the associated checkbox is checked or not. 
00203              * The checkbox's checked property is bound to this datacolumn!
00204              */
00205             if (e.Row.Cells.Count > 1)
00206                 e.Row.Cells[1].Visible = false;
00207         }
00208 
00209         #endregion
00210         #region Private helper methods
00211 
00212         private void importSelectedManufacturers()
00213         {
00214             try
00215             {
00216                                 string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Integrated Security=True;";
00217                                 //string connectionString = string.Format(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Persist Security Info=True;User ID={0};Password={1}",
00218                                 //                                        Session[RAIS.UI.Common.Constants.IcSRS_LOGIN_TAG], Session[RAIS.UI.Common.Constants.IcSRS_PASSWORD_TAG]);
00219                 // Connect to the database
00220                 SqlConnection con = new SqlConnection(connectionString);
00221                 con.Open();
00222 
00223                 // Check each row for import
00224                 foreach (DataRow row in ((DataTable)Session["SearchResult"]).Rows)
00225                 {
00226                     // Check if row is selected, if so -> import Manufacturer
00227                     if ((bool)row[0] == true)
00228                     {
00229                         #region SET VALUES
00230 
00231                         /* Set the attributes values to the values of the current row
00232                          * Defaul value is NULL!
00233                          */
00234 
00235                         string company = "'" + row["Company"].ToString() + "'";
00236 
00237                         string address = "NULL";
00238                         if (row.Table.Columns.Contains("Address1") && !row["Address1"].ToString().Equals(""))
00239                             address = "'" + row["Address1"].ToString() + "'";
00240 
00241                         string telephone = "NULL";
00242                         if (row.Table.Columns.Contains("Telephone1") && !row["Telephone1"].ToString().Equals(""))
00243                             telephone = "'" + row["Telephone1"].ToString() + "'";
00244 
00245                         string fax = "NULL";
00246                         if (row.Table.Columns.Contains("Fax") && !row["Fax"].ToString().Equals(""))
00247                             fax = "'" + row["Fax"].ToString() + "'";
00248 
00249                         string email = "NULL";
00250                         if (row.Table.Columns.Contains("E-Mail") && !row["E-Mail"].ToString().Equals(""))
00251                             email = "'" + row["E-Mail"].ToString() + "'";
00252 
00253                         #endregion
00254 
00255                         #region GET COUNTRY FK
00256 
00257                         /* Get the current country foreign key which is needed for adding a new manufacturer
00258                          * Saves the country FK to a string ("countryFK")
00259                          */
00260 
00261                         string country = "'" + row["Country"].ToString() + "'";
00262                         if (country.Equals("'United States of America'"))
00263                             country = "'USA'";
00264 
00265                         SqlCommand cmd = new SqlCommand("SELECT \"PK Country ID\" FROM Country WHERE \"Country Name\" = " + country, con);
00266                         SqlDataReader reader = cmd.ExecuteReader();
00267                         string countryFK = "NULL";
00268                         reader.Read();
00269                         if (reader.HasRows)
00270                             countryFK = reader["PK Country ID"].ToString();
00271                         reader.Close();
00272                         #endregion
00273 
00274                         #region CHECK MANUFACTURER NOT IN LIST
00275 
00276                         /* Checks if the selected manufacturer is already in the list.
00277                          * A manufacturer is already in the list if his name and his address
00278                          * are equal to another datarow in the DB!
00279                          */
00280 
00281                         bool alreadyExists = false;
00282                         cmd = new SqlCommand("SELECT * FROM Manufacturer WHERE Name = " + company, con);
00283                         reader = cmd.ExecuteReader();
00284                         while (reader.Read())
00285                         {
00286                             if (reader["Address"].Equals(address)) // Manufacturer already exists!
00287                                 alreadyExists = true;
00288                         }
00289                         reader.Close();
00290 
00291                         #endregion
00292 
00293                         #region IMPORT MANUFACTURER
00294 
00295                         if (!alreadyExists) // create new manufacturer
00296                         {
00297                             reader.Close();
00298                             if (countryFK.Equals("NULL"))
00299                                 cmd = new SqlCommand("INSERT INTO Manufacturer (Name, Address, Phone, Fax, eMail) VALUES (" + company + ", " + address + ", " + telephone + ", " + fax + ", " + email + ")", con);
00300                             else
00301                                 cmd = new SqlCommand("INSERT INTO Manufacturer (Name, Address, Phone, Fax, eMail, \"FK Country ID\") VALUES (" + company + ", " + address + ", " + telephone + ", " + fax + ", " + email + ", " + countryFK + ")", con);
00302                             cmd.ExecuteNonQuery();
00303                         }
00304                         else // Update existing manufacturer
00305                         {
00306                             reader.Close();
00307                             if (countryFK.Equals("NULL"))
00308                                 cmd = new SqlCommand("UPDATE Manufacturer SET Address = " + address + ", Phone = " + telephone + ", Fax = " + fax + ", eMail = " + email + " WHERE Name = " + company + " AND Address = " + address, con);
00309                             else
00310                                 cmd = new SqlCommand("UPDATE Manufacturer SET Address = " + address + ", Phone = " + telephone + ", Fax = " + fax + ", eMail = " + email + ", \"FK Country ID\" = " + countryFK + " WHERE Name = " + company + " AND Address = " + address, con);
00311                             cmd.ExecuteNonQuery();
00312                         }
00313 
00314                         #endregion
00315                     }
00316                 }
00317 
00318                 // Close connection and display success message
00319                 con.Close();
00320                 LBLsearch.ForeColor = Color.Green;
00321                 LBLsearch.Text = "Successfully imported " + Session["SelectedItemCount"] + " item(s)!";
00322                 BTNimport1.Visible = false;
00323                 GVsearchResult.Visible = false;
00324             }
00325             catch
00326             {
00327                 LBLsearch.ForeColor = Color.Red;
00328                 LBLsearch.Text = "Data import failed!";
00329             }
00330         }
00331 
00332         private void importSelectedDevices()
00333         {
00334             try
00335             {
00336                 // Connect to the database
00337                                 string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Integrated Security=True;";
00338                                 //string connectionString = string.Format(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Persist Security Info=True;User ID={0};Password={1}",
00339                                 //                                        Session[RAIS.UI.Common.Constants.IcSRS_LOGIN_TAG], Session[RAIS.UI.Common.Constants.IcSRS_PASSWORD_TAG]);
00340                                 SqlConnection con = new SqlConnection(connectionString);
00341                 con.Open();
00342 
00343                 // Check each row for import
00344                 foreach (DataRow row in ((DataTable)Session["SearchResult"]).Rows)
00345                 {
00346                     // Check if row is selected, if so -> import device
00347                     if ((bool)row[0] == true)
00348                     {
00349                         // Check if device is not already in the list
00350                         SqlCommand cmd = new SqlCommand("SELECT * FROM \"Asso Model\" WHERE \"Asso Model Name\" = '" + row["DeviceModel"] + "'", con);
00351                         SqlDataReader reader = cmd.ExecuteReader();
00352                         
00353                         if (!reader.HasRows) // add new device
00354                         {
00355                             #region GET DEVICE TYPE FK
00356 
00357                             /* Set the device type foreign key which is needed for import
00358                              * If it's not existing yet -> create new one
00359                              */
00360 
00361                             cmd = new SqlCommand("SELECT \"PK Asso Type ID\" FROM \"Asso Type\" WHERE \"Type Name\" = '" + row["DeviceType"] + "'", con);
00362                             reader.Close();
00363                             reader = cmd.ExecuteReader();
00364                             string deviceFK = null;
00365                             reader.Read();
00366                             if (reader.HasRows) // Device type exists!
00367                                 deviceFK = reader["PK Asso Type ID"].ToString();
00368                             else // Add new device type
00369                             {
00370                                 reader.Close();
00371                                 cmd = new SqlCommand("INSERT INTO \"Asso Type\" (\"Type Name\") VALUES ('" + row["DeviceType"] + "')", con);
00372                                 cmd.ExecuteNonQuery();
00373 
00374                                 // get new device type FK
00375                                 cmd = new SqlCommand("SELECT \"PK Asso Type ID\" FROM \"Asso Type\" WHERE \"Type Name\" = '" + row["DeviceType"] + "'", con);
00376                                 reader = cmd.ExecuteReader();
00377                                 reader.Read();
00378                                 if (reader.HasRows)
00379                                     deviceFK = reader["PK Asso Type ID"].ToString();
00380                             }
00381 
00382                             #endregion
00383 
00384                             #region GET MANUFACTURER FK
00385 
00386                             /* Sets the device type foreign key.
00387                              * If it's non existent -> create new manufacturer
00388                              */
00389 
00390                             cmd = new SqlCommand("SELECT \"PK Manufacturer ID\" FROM Manufacturer WHERE Name = '" + row["Manufacturers"] + "'", con);
00391                             reader.Close();
00392                             reader = cmd.ExecuteReader();
00393                             string manufacturerFK = "NULL";
00394                             reader.Read();
00395                             if (reader.HasRows) // manufacturer exists!
00396                             {
00397                                 manufacturerFK = reader["PK Manufacturer ID"].ToString();
00398                                 reader.Close();
00399                             }
00400                             else // add new manufacturer
00401                             {
00402                                 reader.Close();
00403                                 cmd = new SqlCommand("INSERT INTO Manufacturer (Name) VALUES ('" + row["Manufacturers"] + "')", con);
00404                                 cmd.ExecuteNonQuery();
00405                                 
00406                                 // get new manufacturer FK
00407                                 cmd = new SqlCommand("SELECT \"PK Manufacturer ID\" FROM Manufacturer WHERE Name = '" + row["Manufacturers"] + "'", con);
00408                                 reader = cmd.ExecuteReader();
00409                                 reader.Read();
00410                                 if (reader.HasRows)
00411                                     manufacturerFK = reader["PK Manufacturer ID"].ToString();
00412                                 reader.Close();
00413                             }
00414 
00415                             #endregion
00416 
00417                             #region INSERT NEW DEVICE
00418 
00419                             cmd = new SqlCommand("INSERT INTO \"Asso Model\" (\"Asso Model Name\", \"FK Asso Type ID\", \"FK Manufacturer ID\") VALUES ('" + row["DeviceModel"] + "', '" + deviceFK + "', '" + manufacturerFK + "')", con);
00420                             cmd.ExecuteNonQuery();
00421 
00422                             #endregion
00423                         }
00424                         reader.Close();
00425                     }
00426                 }
00427 
00428                 // Close connection and display success message
00429                 con.Close();
00430                 LBLsearch.ForeColor = Color.Green;
00431                 LBLsearch.Text = "Successfully imported " + Session["SelectedItemCount"] + " item(s)!";
00432                 BTNimport1.Visible = false;
00433                 GVsearchResult.Visible = false;
00434             }
00435             catch
00436             {
00437                 LBLsearch.ForeColor = Color.Red;
00438                 LBLsearch.Text = "Data import failed!";
00439             }
00440         }
00441 
00442         private void importSelectedSources()
00443         {
00444             try
00445             {
00446                 // Connect to the database
00447                                 string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Integrated Security=True;";
00448                                 //string connectionString = string.Format(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RAIS;Persist Security Info=True;User ID={0};Password={1}",
00449                                 //                                        Session[RAIS.UI.Common.Constants.IcSRS_LOGIN_TAG], Session[RAIS.UI.Common.Constants.IcSRS_PASSWORD_TAG]);
00450                                 SqlConnection con = new SqlConnection(connectionString);
00451                 con.Open();
00452 
00453                 // Check each row for import
00454                 foreach (DataRow row in ((DataTable)Session["SearchResult"]).Rows)
00455                 {
00456                     // Check if row is selected, if so -> import source
00457                     if ((bool)row[0] == true)
00458                     {
00459                         // Check if source is not already in the list
00460                         SqlCommand cmd = new SqlCommand("SELECT * FROM \"Sealed Model\" WHERE \"Sealed Model Name\" = '" + row["SourceModel"] + "'", con);
00461                         SqlDataReader reader = cmd.ExecuteReader();
00462 
00463                         // Check if source is not existing yet. If so -> add new source
00464                         if (!reader.HasRows)
00465                         {
00466                             #region GET MANUFACTURER FK
00467 
00468                             /* Sets the device type foreign key.
00469                              * If it's non existent -> create new manufacturer
00470                              */
00471 
00472                             cmd = new SqlCommand("SELECT \"PK Manufacturer ID\" FROM Manufacturer WHERE Name = '" + row["Manufacturers"] + "'", con);
00473                             reader.Close();
00474                             reader = cmd.ExecuteReader();
00475                             string manufacturerFK = "NULL";
00476                             reader.Read();
00477                             if (reader.HasRows) // manufacturer exists!
00478                             {
00479                                 manufacturerFK = reader["PK Manufacturer ID"].ToString();
00480                                 reader.Close();
00481                             }
00482                             else // add new manufacturer
00483                             {
00484                                 reader.Close();
00485                                 cmd = new SqlCommand("INSERT INTO Manufacturer (Name) VALUES ('" + row["Manufacturers"] + "')", con);
00486                                 cmd.ExecuteNonQuery();
00487 
00488                                 // get new manufacturer FK
00489                                 cmd = new SqlCommand("SELECT \"PK Manufacturer ID\" FROM Manufacturer WHERE Name = '" + row["Manufacturers"] + "'", con);
00490                                 reader = cmd.ExecuteReader();
00491                                 reader.Read();
00492                                 if (reader.HasRows)
00493                                     manufacturerFK = reader["PK Manufacturer ID"].ToString();
00494                                 reader.Close();
00495                             }
00496 
00497                             #endregion
00498 
00499                             #region INSERT NEW SOURCE
00500 
00501                             cmd = new SqlCommand("INSERT INTO \"Sealed Model\" (\"Sealed Model Name\", \"FK Manufacturer ID\") VALUES ('" + row["SourceModel"] + "', '" + manufacturerFK + "')", con);
00502                             cmd.ExecuteNonQuery();
00503 
00504                             #endregion
00505                         }
00506 
00507                         reader.Close();
00508                     }
00509                 }
00510 
00511                 // Close connection and display success message
00512                 con.Close();
00513                 LBLsearch.ForeColor = Color.Green;
00514                 LBLsearch.Text = "Successfully imported " + Session["SelectedItemCount"] + " item(s)!";
00515                 BTNimport1.Visible = false;
00516                 GVsearchResult.Visible = false;
00517             }
00518             catch
00519             {
00520                 LBLsearch.ForeColor = Color.Red;
00521                 LBLsearch.Text = "Data import failed!";
00522             }
00523         }
00524 
00525         #endregion
00526     }
00527 }